package v1

import (
	"github.com/gin-gonic/gin"
	"jmicro/app/jshop/api/internal/controller"
	"jmicro/app/jshop/api/internal/domain/request"
	gin2 "jmicro/app/pkg/translator/gin"
	metav1 "jmicro/pkg/common/meta/v1"
	"net/http"
	"strconv"
)

func (us *userServer) List(ctx *gin.Context) {
	pn := ctx.DefaultQuery("pn", "0")
	pnInt, _ := strconv.Atoi(pn)
	pSize := ctx.DefaultQuery("psize", "10")
	pSizeInt, _ := strconv.Atoi(pSize)
	list, err := us.userSrv.List(ctx, metav1.ListMeta{pnInt, pSizeInt})
	if err != nil {
		gin2.HandleValidatorError(ctx, err, controller.Trans)
	}
	result := make([]interface{}, 0)
	for _, value := range list.Items {
		user := request.UserResponse{
			Id:       int32(value.ID),
			NickName: value.NickName,
			//Birthday: time.Time(time.Unix(int64(value.BirthDay), 0)).Format("2006-01-02"),
			Birthday: value.Birthday,
			Gender:   value.Gender,
			Mobile:   value.Mobile,
		}
		result = append(result, user)
	}

	ctx.JSON(http.StatusOK, result)
}
